// https://dao3.fun/edit/f2658c7f42f229b6ddc9
// let voxelDict = [];

// for (let x = 0; x < 128; x++) {
//     for (let y = 0; y < 128; y++) {
//         for (let z = 0; z < 128; z++) {
//             let voxelId = voxels.getVoxelId(x, y, z);
//             if (voxelId !== 0) {voxelDict.push([x, y, z,voxelId]);}
//         }
//     }
// }
// https://dao3.fun/play/90377378b4f8c01eec95?from_dream

import './Start.ts'
import './interactThings.ts'
import './InteractBed.ts'
import './Granny&Grandpa.ts'
import './keys.ts'
import './i18n.ts'
import { translate } from './i18n'
import { globalText } from './interactThings'

//--------New CameraMode Fixed--------//
world.onPlayerJoin(async ({ entity }) => {
    entity.player.cameraMode = GameCameraMode.FIXED
    entity.player.cameraPosition = new GameVector3(55.5, 9.6, 97.1)
    entity.player.cameraFovY = 20.5 // 开启广角
    entity.player.cameraTarget = new GameVector3(60.9, 11.8, 92.8)
})
//--------New CameraMode Fixed--------//

world.onPlayerJoin(async ({ entity }) => {
    entity.chose = ''
    if (String(entity.player.url) === 'https://dao3.fun/play/90377378b4f8c01eec95?from_dream') {
        entity.player.directMessage(translate('欢迎回来', entity.language || 'zh'))
        world.say(translate('{player}从梦魇回来辣', entity.language || 'zh', { player: entity.player.name }))
    }
})

world.onPlayerJoin(async ({ entity }) => {
    if (entity.player.name == '坦率的血翼蝠5801' || entity.player.name == '暗23') {
        entity.player.setSkinByName('sans');
    }
    entity.player.onPress(async ({ entity, button }) => {
        if (button == 'action1') {
            await entity.player.dialog({
                type: GameDialogType.SELECT,
                title: translate('菜单', entity.language || 'zh'),
                content: translate('你现在拿着{onHand}，你拥有的能力有↓', entity.language || 'zh', { onHand: translate(entity.onHand, entity.language || 'zh') }),
                options: entity.bq.map((ability: string) => translate(ability, entity.language || 'zh'))
            })
        }
    })
})


const MeshScale = new GameVector3(0.0625, 0.0625, 0.0625)
async function spawn_fish() {//把生成敌人的代码封装成函数
    const e = world.createEntity({
        mesh: 'mesh/鳄鱼.vb',
        meshScale: MeshScale,
        collides: true, // 开启碰撞
        gravity: false, // 开启重力
        friction: 0, // 关闭摩擦力
        fixed: false,
        position: new GameVector3(
            8.8, 8.8, 8.2
        ),
    });
    if (e == null) {
        world.say(translate('鳄鱼生成失败', 'zh'))
        return;
    }
    e.hp = 10000
    e.maxHp = 10000
    e.addTag('鳄鱼')
    e.enableDamage = true;
    // e.onVoxelContact(({x,y,z,voxel})=>{
    //     e.velocity.y = 0.3+Math.random()*0.7//碰到砖块就跳一下
    // })
    e.onEntityContact(async ({ other }) => {
        if (other.player) {//鳄鱼撞到玩家
            if (other.hasTag('cannotDie')) return;
            if (other.hp <= 0) return;
            other.hurt(100)//对玩家造成伤害
            other.player.directMessage(translate('你被鳄鱼袭击，剩余血量{hp}/{maxHp}', other.language || 'zh', { hp: other.hp, maxHp: other.maxHp }))
        }
    })

    e.onDie(async () => {
        e.destroy()
        world.say(translate('鳄鱼离开了1秒', 'zh'))
        await sleep(1000)
        spawn_fish()
    })
    while (true) {
        if (e.position.y >= 9) {
            e.destroy()
            world.say(translate('鳄鱼回游！', 'zh'))
            spawn_fish()
            break;
            return;
        }
        await sleep(640)
    }
}

world.onChat(async ({ message, entity }) => {
    if (!entity.player) return;
    if (entity.player.name == '坦率的血翼蝠5801') {
        // 聊天命令使用原始中文文本，不翻译
        if (message == '鳄鱼回游') {
            world.querySelectorAll('.鳄鱼').forEach((e) => {
                e.destroy()
                world.say(translate('鳄鱼回游！', 'zh'))
                spawn_fish()
            })

        }
    }
})

// console.log(voxelDict);
// world.querySelectorAll('player').forEach((e) => {e.player.link('https://dao3.fun/edit/8a2109ec58264c4f2805')})
// world.querySelectorAll('player').forEach((e) => {e.player.link('https://box3.codemao.cn/e/66088fd78d34bbc9c8d4')})
// world.querySelectorAll('player').forEach((e) => {e.removeTag('吸引')})

world.querySelectorAll('.invisible').forEach((e) => { e.meshInvisible = true })

world.onPlayerJoin(async ({ entity }) => {
    entity.wq = 0
    entity.addTag('noTree')
    entity.position.copy(entity.player.spawnPoint)
    entity.enableDamage = true
    entity.player.directMessage(translate('欢迎来到不一样的恐怖奶奶3', entity.language || 'zh'))
    entity.player.runSpeed = 0
    entity.player.enableDoubleJump = false
    entity.addTag('noBridge')
    // entity.onFluidEnter(async({entity}) => {
    //     entity.hurt(entity.maxHp)
    // })
    entity.day = 1
    /*
    
    */
})
world.onPlayerJoin(async ({ entity }) => {
    entity.enableDamage = true;
    entity.onHand = '空气'

    world.gravity = -0.14
    world.rainSizeHi = 0.2
    world.maxFog = 0.5;
    world.fogColor = new GameRGBColor(1, 1, 1);
    world.fogHeightOffset = -0.5;
    world.fogStartDistance = 0.5;
    world.fogUniformDensity = 1;
    world.fogHeightFalloff = 0;



})

var code = world.querySelector('#公告栏-1')
if (code) {
    code.enableInteract = true
    code.interactRadius = 2.5
    code.interactHint = globalText('前往-噩梦模式');
    code.onInteract(({ entity }) => {
        entity.player.link('https://dao3.fun/play/c18de23cba039f9c6d36')
    })
}


const teleport_ = world.querySelector('#单机')
if (teleport_) {
    teleport_.enableInteract = true
    teleport_.interactRadius = 6
    teleport_.interactHint = globalText('我要玩单机游戏')

    teleport_.onInteract(async ({ entity }) => {
        if (entity.player.userId === '' || entity.player.userId === '0' || entity.player.userId === null) {
            entity.player.directMessage(translate('抱歉！游客无法体验单机模式！', entity.language || 'zh'))
            return;
        }
        const standalone = await entity.player.dialog({
            type: GameDialogType.SELECT,
            title: translate('单机版本', entity.language || 'zh'),
            content: translate('我要玩单机版本，玩得清净！', entity.language || 'zh'),
            options: [translate('简单模式', entity.language || 'zh'), translate('正常模式', entity.language || 'zh'), translate('困难模式(未上架)', entity.language || 'zh'), translate('极限模式(已下架)', entity.language || 'zh'), translate('梦魇模式', entity.language || 'zh')]
        })
        if (standalone) {
            switch (standalone.value) {
                case translate('简单模式', entity.language || 'zh'):
                    entity.chose = '100174245'
                    break;
                case translate('正常模式', entity.language || 'zh'):
                    entity.chose = '100183839'
                    break;
                case translate('梦魇模式', entity.language || 'zh'):
                    entity.chose = '100181304'
                    break;
                default:
                    return; // 处理无效选项
            }

            while (true) {
                try {
                    let players = [entity]
                    players = players.filter(e => e.player.userId !== '' && e.player.userId !== '0' && e.player.userId !== null)
                    await world.teleport(entity.chose, players)
                    break
                } catch (e: any) {
                    world.say(translate('传送失败：', entity.language || 'zh') + e.stack)
                    console.log('传送失败：' + e.stack)
                }
                await sleep(1000)
            }
        }
    })
}